package com.petalsgroup.petals.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("/admin/*")
public class LoginFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req=(HttpServletRequest) request;
        String requestURI = req.getRequestURI();
        if (requestURI.contains("/toLogin")||requestURI.contains("/css/")
                ||requestURI.contains("/checkCode")||requestURI.contains("/js/")
                ||requestURI.contains("/adminLogin")||requestURI.contains("/fonts/")){
            chain.doFilter(request, response);
        }else {
            Object user = req.getSession().getAttribute("loginAdmin");
            if (user!=null){
                chain.doFilter(request, response);
            }
            else {
                request.getRequestDispatcher("/admin/toLogin").forward(req,response);
            }
        }
    }
}
